$(function() {
	let bodyHeight = $('body').height();
	bodyHeight *= 0.6;
	
	$('.icon').click(function() { //功能按钮
		const key = $(this).data('key');
		let userId = localStorage.userId;
		if(userId) {
			switch(key) {
				case 'SK': //收款
					Util.jump('receivables.html');
					break;
				case 'DD': //我的订单
					Util.jump('orderList.html');
					break;
				case 'KF': //帮助中心
					Util.jump(Host + '/helpadmin/home/search.html?userId=' + userId);
					break;
				case 'AUTH': //认证中心
					Util.jump('Auth.html');
					break;
			}
		} else {
			layer.open({
				content: '请登录后使用',
				skin: 'msg',
				time: 2
			});
		}
	});
	
	$('.model').click(function() { //触摸注册框外区域关闭model框
		$('.model').hide();
		$('.modelcont').hide();
		$('.modellogin').hide();
	});
	
	$('.agreement img').click(function() { //阅读注册协议
		if($(this).attr('src') == 'img/checked.png') {
			$(this).attr('src', 'img/unchecked.png');
		} else {
			$(this).attr('src', 'img/checked.png');
		}
	});
	
	$('.register').click(function() { //注册model打开
		$('.model').show();
		$('.modelcont').show();
	});

	function isAgreement() { //验证手机是否注册方法封装
		const phone = $('#phone').val(); // 手机
		if(!phone.match(/^(1)[0-9]{10}$/)) {
			layer.open({
				content: '手机格式不正确',
				skin: 'msg',
				time: 3,
				top: -bodyHeight
			});
			return false;
		} else {
			$.ajax({
				type: 'post',
				url: Host + '/app/user/checkExist.htm',
				contentType: 'application/json; charset=utf-8',
				data: JSON.stringify({
					"phone": phone
				}),
				dataType: 'json',
				success: function(data) {
					if(data.code == 50006) {
						layer.open({
							content: '手机号已经注册',
							skin: 'msg',
							time: 3,
							top: -bodyHeight
						});
						// 如果手机号已经注册，那么不发送验证码
						return false;
					} else if(data.code == 200) {
						// 手机符合规则，获取验证码响应事件
						verify($('.verification-btn'));
					}
				}
			});
		}
	};
	
	// 获取验证码，验证手机是否已注册
	$('.verification-btn').on('click', function() {
		isAgreement();
	});
	
	// 注册按钮响应事件
	$('#submit').on('click', function() {
		vilidate();
	});
	
	// 如果手机号符合规则,可以点击获取验证码 开始倒计时
	function verify(self) {
		var phone = $('#phone').val(); // 手机号
		if(!phone.match(/^(1)[0-9]{10}$/)) {
			layer.open({
				content: '手机格式不正确',
				skin: 'msg',
				time: 3,
				top: -bodyHeight
			});
			return false;
		}
		$.ajax({
			type: 'post',
			url: Host + '/common/sms/sendVerify.htm',
			contentType: 'application/json; charset=utf-8',
			data: JSON.stringify({
				'phone': phone,
				bizType: 0
			}),
			dataType: 'json',
			success: function(data) {
				if(data.code == 200) {
					layer.open({
						content: '验证码已发送',
						skin: 'msg',
						time: 3,
						top: -bodyHeight
					});
				} else {
					layer.open({
						content: data.message,
						skin: 'msg',
						time: 3,
						top: -bodyHeight
					})
				}
				var time = 60;
				$(self).css('background-color', '#f0f0f0');
				$(self).off('click');
				var timer = setInterval(function() {
					if(time == 1) {
						time = 60;
						$(self).text('重新发送');
						$(self).css('backgroundColor', '#fff');
						clearInterval(timer);
						$(self).on('click', function() {
							isAgreement();
						});
					} else {
						time--;
						$(self).text(time + 's后重试');
					}
				}, 1000);
			}
		});
	}

	function vilidate() {
		var phone = $('#phone').val(); // 手机
		var pwd = $('#password').val(); // 密码
		var verify = $('#verification').val(); // 验证码
		var pact_checked = $('.agreement img').attr('src'); // 协议
		var reCode = Util.GetQueryString('reCode'); //分享人的userId
		var topUserId = Util.GetQueryString('topUserId'); //顶级userId
		if(!phone.match(/^(1)[0-9]{10}$/)) {
			layer.open({
				content: '手机格式不正确',
				skin: 'msg',
				time: 3,
				top: -bodyHeight
			})
		} else if($.trim(pwd) == '') {
			layer.open({
				content: '请设置登录密码',
				skin: 'msg',
				time: 3,
				top: -bodyHeight
			})
		} else if(!verify) {
			layer.open({
				content: '请输入验证码',
				skin: 'msg',
				time: 3,
				top: -bodyHeight
			})
		} else if(pact_checked == 'img/unchecked.png') {
			layer.open({
				content: '请勾选协议',
				skin: 'msg',
				time: 3,
				top: -bodyHeight
			})
		} else {
			var param = {
				phone: phone,
				password: $.base64.encode(pwd),
				verifyCode: verify,
				reCode: reCode,
				topUserId: topUserId
			};
			$.ajax({
				type: 'post',
				url: Host + '/app/user/register.htm',
				contentType: 'application/json; charset=utf-8',
				data: JSON.stringify(param),
				dataType: 'json',
				success: function(data) {
					if(data.code == 200) {
						$('.model').hide();
						$('.modelcont').hide();
						layer.open({
							content: '恭喜您，注册成功！',
							skin: 'msg',
							time: 3,
							top: -bodyHeight
						});
					} else {
						layer.open({
							content: data.message,
							skin: 'msg',
							time: 3,
							top: -bodyHeight
						})
					}
				}
			});
		}
	}
	
	$('.login').click(function(){ //登录
		$('.model').show();
		$('.modellogin').show();
	});
	
	$('#loginsb').click(function(){ //确认登录
		let phone = $('#login_phone').val();
		let password = $('#login_password').val();
		if(!phone.match(/^(1)[0-9]{10}$/)){
			layer.open({
				content: '手机格式不正确',
				skin: 'msg',
				time: 3,
				top: -bodyHeight
			})
		}else if(password == ''){
			layer.open({
				content: '密码不能为空',
				skin: 'msg',
				time: 3,
				top: -bodyHeight
			})
		}else{
			let param = {
				phone: phone,
				password: $.base64.encode(password)
			};
			$.ajax({
				type: 'post',
				url: Host + '/app/user/login.htm',
				contentType: 'application/json; charset=utf-8',
				data: JSON.stringify(param),
				dataType: 'json',
				success: function(res) {
					console.log(res);
					if(res.code == 200) {
						localStorage.userId = res.data.id; //用户id
					    localStorage.userName = res.data.userName; //用户姓名
					    localStorage.cardNo = res.data.cardNo; //用户身份证号码
						$('.model').hide();
						$('.modellogin').hide();
						$('.login').hide();
						$('.register').hide();
						if(res.data.userName != ''){
							$('.login_on').show().text('Hi,'+res.data.userName);
						}else{
							$('.login_on').show().text('Hi,'+res.data.id);
						}
						$('.down').show();
						layer.open({content: '恭喜您，登录成功！',skin: 'msg',time: 2});
					} else {
						layer.open({
							content: res.message,
							skin: 'msg',
							time: 3,
							top: -bodyHeight
						})
					}
				}
			});
		}
	});
	
	$('.login_on').click(function(){
			layer.open({
				    content: '是否确认退出登录?'
				    ,btn: ['确认', '不要']
				    ,yes: function(index){
				      localStorage.userId = '';
				      var urlCode = Util.generateMixed(10);
					  window.location.href=window.location.href+"?Code="+urlCode;
				      layer.close(index);
				    }
				  });
	});
	
	$('.down').click(function(){
		layer.open({
				    content: '体验更多功能，请下载APP'
				    ,btn: ['去下载', '不要']
				    ,yes: function(index){
				      Util.jump('http://www.uniao.me/share/downLoadPage.htm?topUserId=2000');
				      layer.close(index);
				    }
				  });
	});
	
})